﻿using System;
using System.Data;
using System.Web;
using System.Text.RegularExpressions;

using LMD.Lib.DataClass;
using LMD.Lib.Interface;
using LMD.Lib.Controls;
using LMD.Constant;
/// <summary>
/// This class contains extension methods
/// </summary>
public static class Extensions
{
	public static String PutLineBreaks(this String strData)
    {
        return new Regex("(?<!\r)\n").Replace(strData, "<br/>");
    }
    
    public static String EscapeForURL(this String strData)
    {
        return String.IsNullOrEmpty(strData) ? strData :
            Microsoft.JScript.GlobalObject.escape(strData);
    }

    public static String ReplaceQuotes(this String strData)
    {
        return String.IsNullOrEmpty(strData) ? strData : strData.Replace("\"", "&#34;").Replace("'", "&#39;");
    }

    public static String ReplaceQuotesForJS(this String strData)
    {
        return String.IsNullOrEmpty(strData) ? strData : strData.Replace("\"", "&#34;").Replace("'", "\\'");
    }

    public static String ReplaceBraceTag(this String strData)
    {
        return String.IsNullOrEmpty(strData) ? 
            strData :
            strData.Replace("<", KGlobal.LEFTBRACE).Replace(">", KGlobal.RIGHTBRACE).Replace("/", KGlobal.SLASH);
    }

    public static String AddSpacesIfNotEmpty(this String strData)
    {
        return String.IsNullOrEmpty(strData) ? strData : strData + " ";
    }

    #region QueryString Result
    public static String ReadQuery(this HttpContext context, String sxParamName)
    {
        return String.IsNullOrEmpty(context.Request.QueryString[sxParamName]) ? 
            String.Empty : context.Request.QueryString[sxParamName];
    }

    public static int ReadInt(this HttpContext context, String sxParamName)
    {
        return ReadInt(context, sxParamName, 0);
    }
    
    public static int ReadInt(this HttpContext context, String sxParamName, int iDefault)
    {
        int iRet = iDefault;
        try
        {
            iRet = String.IsNullOrEmpty(context.Request.QueryString[sxParamName]) ?
                iDefault : int.Parse(context.Request.QueryString[sxParamName]);
        }
        catch { /* LET IT BE ZERO */ }

        return iRet;
    }
    #endregion QueryString Result

    #region DataBase Result
    public static int ReadInt(this DataRow dr, String sxColumnName)
    {
        return ReadInt(dr, sxColumnName, -1);
    }
    public static int ReadInt(this DataRow dr, String sxColumnName, int iDefaultValue)
    {
        return dr[sxColumnName] == DBNull.Value ? iDefaultValue : Convert.ToInt32(dr[sxColumnName]);
    }

    public static String ReadString(this DataRow dr, String sxColumnName)
    {
        return ReadString(dr, sxColumnName, String.Empty);
    }
    public static String ReadString(this DataRow dr, String sxColumnName, String sxDefaultValue)
    {
        return dr[sxColumnName] == DBNull.Value ? sxDefaultValue : dr[sxColumnName].ToString();
    }

    public static DateTime ReadDateTime(this DataRow dr, String sxColumnName)
    {
        return ReadDateTime(dr, sxColumnName, DateTime.MinValue);
    }
    public static DateTime ReadDateTime(this DataRow dr, String sxColumnName, DateTime dtmDefaultValue)
    {
        return dr[sxColumnName] == DBNull.Value ? dtmDefaultValue : Convert.ToDateTime(dr[sxColumnName]);
    }
    #endregion DataBase Result

    public static String GetString(this Gender gdr)
    {
        switch (gdr)
        {
            case Gender.Male: return "Laki-laki";
            case Gender.Female: return "Perempuan";
            default: return String.Empty;
        }
    }

    public static String GetEditor(this ILOVProvider lovProvider, string sxControlId, 
        string sxValue, string sxOldDataTypeId, String sxCustomParam)
    {
        String sxReturn = String.Empty;
        String sxLOVName = lovProvider.GetLOVName();
        TextboxType tpTextbox = CustomControls.GetTextBoxType(sxLOVName,sxValue);

        if (int.Parse(sxOldDataTypeId) == (int)tpTextbox)
            return sxReturn; // no need to change the editor, it's the same

        sxReturn = ((int)tpTextbox).ToString() + KGlobal.DELIMITER + ": ";
        sxReturn += CustomControls.Create_TextBox(sxControlId, KControl.INPUT_ID, null, true, tpTextbox);

        return sxReturn;
    }
    
    

    public static String RandomDateString()
    {
        DateTime dtm = DateTime.Now;
        int iRand = new Random().Next(9999);

        return dtm.ToString("yyyyMMddHHmmssfff") + iRand.ToString().PadLeft(4, 'X');
    }
}
